package com.numberone.iotplatform.controller;

import com.numberone.iotplatform.dto.DeviceDTO;
import com.numberone.iotplatform.dto.DeviceDetailDTO;
import com.numberone.iotplatform.dto.DeviceFormDTO;
import com.numberone.iotplatform.entity.AiModel; // 引入 AiModel
import com.numberone.iotplatform.entity.Channel; // 引入 Channel
import com.numberone.iotplatform.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Set; // 引入 Set

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/devices")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    // 获取设备列表（分页、筛选）
    @GetMapping
    public ResponseEntity<Page<DeviceDTO>> getDevices(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Long locationId,
            Pageable pageable
    ) {
        Page<DeviceDTO> devices = deviceService.findAll(keyword, status, locationId, pageable);
        return ResponseEntity.ok(devices);
    }

    // 获取设备核心详情
    @GetMapping(value = "/{id}")
    public ResponseEntity<DeviceDetailDTO> getDeviceDetail(@PathVariable Long id) {
        DeviceDetailDTO detail = deviceService.getDeviceDetailById(id);
        return ResponseEntity.ok(detail);
    }

    /**
     * 【新增】获取指定设备的视频通道列表
     * @param id 设备ID
     * @return 该设备下的通道集合
     */
    @GetMapping("/{id}/channels")
    public ResponseEntity<Set<Channel>> getDeviceChannels(@PathVariable Long id) {
        Set<Channel> channels = deviceService.findChannelsByDeviceId(id);
        return ResponseEntity.ok(channels);
    }

    /**
     * 【新增】获取指定设备的AI模型列表
     * @param id 设备ID
     * @return 该设备下的模型集合
     */
    @GetMapping("/{id}/models")
    public ResponseEntity<Set<AiModel>> getDeviceModels(@PathVariable Long id) {
        Set<AiModel> models = deviceService.findAiModelsByDeviceId(id);
        return ResponseEntity.ok(models);
    }

    @PostMapping
    public ResponseEntity<DeviceDTO> createDevice(@Valid @RequestBody DeviceFormDTO dto) {
        return ResponseEntity.ok(deviceService.createDevice(dto));
    }

    @PutMapping("/{id}")
    public ResponseEntity<DeviceDTO> updateDevice(@PathVariable Long id, @Valid @RequestBody DeviceFormDTO dto) {
        return ResponseEntity.ok(deviceService.updateDevice(id, dto));
    }

    /**
     * 【核心补全】暴露一个用于重启设备的 API 接口。
     * 对应前端的“重启设备”按钮点击事件。
     *
     * @param id 要重启的设备 ID
     * @return 成功时返回 200 OK，无内容
     */
    @PostMapping("/{id}/reboot")
    public ResponseEntity<Void> rebootDevice(@PathVariable Long id) {
        deviceService.rebootDeviceById(id); // 调用 Service 层的业务逻辑
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteDevice(@PathVariable Long id) {
        deviceService.deleteDeviceById(id);
        return ResponseEntity.noContent().build(); // 返回 204 No Content 更符合 RESTful 规范
    }
}